<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>文件列表</title>
    <base th:href="${#httpServletRequest.getContextPath()} + '/'">
    <!-- 引入 layui.css -->
    <link rel="stylesheet" href="layui/css/layui.css">
    <!-- 引入 layui.js -->
    <script src="layui/layui.js"></script>
</head>
<body style="width: 99%">

<table class="layui-hide" id="test" lay-filter="test"></table>
</body>
<script type="text/html" id="toolbarDemo">
    <div class="demoTable">
        <div class="layui-inline">
            <div style="display: inline-block;">
                <label for="search_id"></label>
                <input class="layui-input" style="width: 150px" placeholder="ID" id="search_id"
                       autocomplete="off">
            </div>
            <div style="display: inline-block;">
                <label for="search_username"></label>
                <input class="layui-input" style="width: 150px" placeholder="上传者" id="search_username"
                       autocomplete="off">
            </div>
            <div style="display: inline-block;">
                <label for="search_fileName"></label>
                <input class="layui-input" style="width: 150px" placeholder="文件名" id="search_fileName"
                       autocomplete="off">
            </div>
            <div style="display: inline-block;">
                <label for="search_beginDate"></label>
                <input class="layui-input" style="width: 150px" placeholder="起始日期" id="search_beginDate"
                       autocomplete="off">
            </div>
            <div style="display: inline-block;">
                <span>&nbsp;至&nbsp;</span>
            </div>
            <div style="display: inline-block;">
                <label for="search_endDate"></label>
                <input class="layui-input" style="width: 150px" placeholder="终止日期" id="search_endDate"
                       autocomplete="off">
            </div>
        </div>
        <span>&nbsp;&nbsp;</span>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="download">下载</a>
    <a class="layui-btn layui-btn-xs" lay-event="modify">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script th:inline="none">
    layui.use('table', function () {
        const $ = layui.$

        // 数据表单时间戳转换为日期显示
        function DateFormat(sjc) {
            const date = new Date(sjc);
            const y = date.getFullYear();
            let m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            let d = date.getDate();
            d = d < 10 ? ("0" + d) : d;
            let h = date.getHours();
            h = h < 10 ? ("0" + h) : h;
            let min = date.getMinutes();
            min = min < 10 ? ("0" + min) : min;
            let s = date.getSeconds();
            s = s < 10 ? ("0" + s) : s;
            return y + "-" + m + "-" + d + " " + h + ":" + min + ":" + s;
        }

        const table = layui.table;
        table.render({
            id: 'table',
            elem: '#test'
            , url: 'uploadFile/get/all/uploadFiles.json'
            , toolbar: '#toolbarDemo' // 开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { // 自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 100, fixed: 'left', unresize: true, sort: true}
                , {field: 'filename', title: '文件名', edit: 'text', sort: true}
                , {field: 'username', title: '上传者', width: 120, sort: true}
                , {
                    field: 'uploaddate',
                    title: '上传时间',
                    width: 180,
                    sort: true,
                    templet: function (res) {
                        return DateFormat(res.uploaddate);
                    }
                }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 180, unresize: true}
            ]]
            , page: true
        });

        // 头工具栏事件
        table.on('toolbar(test)', function (obj) {
            switch (obj.event) {
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这里什么都没有');
                    break;
            }
        });

        // 监听行工具事件
        table.on('tool(test)', function (obj) {
            const data = obj.data;
            if (obj.event === 'download') {
                downloadFileByForm(data.id);
            } else if (obj.event === 'del') {
                layer.confirm('确定要删除 ' + data.filename + ' 吗?', function (index) {
                    $.ajax({
                        url: "uploadFile/remove",
                        type: "post",
                        data: {
                            id: data.id
                        },
                        dataType: "json",
                        success: function (response) {
                            let code = response.code;
                            if (code == 0) {
                                layer.msg("删除成功!", {icon: 1, offset: "auto", time: 3000});
                                obj.del();
                                reload();
                                setTimeout(function () {
                                    let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                    parent.layer.close(index);
                                }, 3000);
                            } else {
                                layer.msg("删除失败! " + response.code + " " + response.msg, {
                                    icon: 0,
                                    offset: "auto",
                                    time: 2000
                                });
                            }
                        },
                        error: function (response) {
                            layer.msg("删除失败! " + response.status + " " + response.statusText, {
                                icon: 0,
                                offset: "auto",
                                time: 2000
                            });
                        }
                    })
                    layer.close(index);
                });
            } else if (obj.event === 'modify') {
                $.ajax({
                    url: "uploadFile/update",
                    type: "post",
                    data: JSON.stringify(data),
                    contentType: "application/json;charset=utf8",
                    dataType: "json",
                    success: function (response) {
                        let code = response.code;
                        if (code == 0) {
                            layer.msg("修改成功!", {icon: 1, offset: "auto", time: 3000});
                            reload();
                            setTimeout(function () {
                                let index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                parent.layer.close(index);
                            }, 3000);
                        } else {
                            layer.msg("修改失败! " + response.code + " " + response.msg, {
                                icon: 0,
                                offset: "auto",
                                time: 2000
                            });
                        }
                    },
                    error: function (response) {
                        layer.msg("修改失败! " + response.status + " " + response.statusText, {
                            icon: 0,
                            offset: "auto",
                            time: 2000
                        });
                    }
                })
            }
        });

        function reload() {
            //执行重载
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    id: $("#search_id").val(),
                    username: $("#search_username").val(),
                    fileName: $("#search_fileName").val(),
                    beginDate: $("#search_beginDate").val(),
                    endDate: $("#search_endDate").val()
                }
            });
            openDateSelector();
        }

        // 搜索
        $('body').on('click', '.demoTable .layui-btn', function () {
            reload();
        });

        openDateSelector();
    })

    // 开启日期选择器
    function openDateSelector() {
        layui.use('laydate', function () {
            let laydate = layui.laydate;
            laydate.render({
                elem: '#search_beginDate'
            });
            laydate.render({
                elem: '#search_endDate'
            });
        });
    }

    // 模拟表单提交同步方式下载文件
    // 能够弹出保存文件对话框
    function downloadFileByForm(id) {
        let url = "uploadFile/download";
        let form = layui.$("<form></form>").attr("action", url).attr("method", "post");
        form.append(layui.$("<input>").attr("type", "hidden").attr("name", "id").attr("value", id));
        form.appendTo('body').submit().remove();
    }
</script>
</html>